//
//  LSZWithdrawalVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/3/28.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZWithdrawalVC.h"

@interface LSZWithdrawalVC ()

@end

@implementation LSZWithdrawalVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title  = @"提现记录";
    if(ScreenWidth ==414){
        cellHeight = 50+10;
    }else
    if(ScreenWidth ==375){
        cellHeight = 50+10;
    }else{
        cellHeight = 40+10;
    }
    [self setTopView];
    [self setLowUI];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, ScreenHeight)];
    [self.view addSubview:view];
    [self.view bringSubviewToFront:view];
    [self getDataWithdrawal];
}
/*
#pragma mark- ui 当开始滚动视图时，执行该方法。一次有效滑动（开始滑动，滑动一小段距离，只要手指不松开，只算一次滑动），只执行一次。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //获取开始位置
    beginContentY = scrollView.contentOffset.y;
}
#pragma mark - 每次滑动 总要执行一次
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    //每次滑动 获取结束位置
    endContentY = scrollView.contentOffset.y;
    if(endContentY-beginContentY > 0&&endContentY>0){
#pragma mark - 只要上拉就消失
        [UIView animateWithDuration:0.25 animations:^{
            CGRect rect = self.navigationController.navigationBar.frame;
            rect.origin.y = -(NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
            self.navigationController.navigationBar.frame = rect;
        }];
        [self.tableViewDeal reloadData];
    }else if(endContentY-beginContentY < 0){
#pragma mark - 只要下拉就显示
        [UIView animateWithDuration:0.25 animations:^{
            CGRect rect = self.navigationController.navigationBar.frame;
            rect.origin.y = STATUS_BAR_HEIGHT;
            self.navigationController.navigationBar.frame = rect;
        } completion:^(BOOL finished) {
            [self.tableViewDeal reloadData];
        }];
    }
}
*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(ScreenWidth ==414){
        return 125;
    }else
    if(ScreenWidth ==375){
        return 120;
    }else{
        return 110;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return cellHeight;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return cellHeight-10;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(self.tableViewDeal == tableView){
        return  self.areDealingManager.arrayData.count;
    }else{
        return  self.WithdrawalRecordManager.arrayData.count;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LSZWithdrawalHeaderView * header =  [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZWithdrawalHeaderView"];
    LSZWithdrawalRecordMg * mg = nil;
    if(self.tableViewDeal == tableView){
        mg = self.areDealingManager.arrayData[section];
    }else{
        mg = self.WithdrawalRecordManager.arrayData[section];
    }
    header.labelState.text = NSStr(mg.status);
    header.labelType.text = [NSString stringWithFormat:@"订单号:%@",mg.trade_no];
    return header;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    LSZWithdrawalFootView * foot = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZWithdrawalFootView"];
    LSZWithdrawalRecordMg * mg = nil;

    if(self.tableViewDeal == tableView){
        mg = self.areDealingManager.arrayData[section];
    }else{
        mg = self.WithdrawalRecordManager.arrayData[section];
    }
    NSString * stringName = [NSString stringWithFormat:@"支付金额:%@福音值",mg.real_amount];
    NSMutableAttributedString * strName = [[NSMutableAttributedString alloc]initWithString:stringName];
    //设置：在0-3个单位长度内的内容显示成红色
    [strName addAttribute:NSForegroundColorAttributeName value:redUIColor range:NSMakeRange(5,stringName.length-5)];
    foot.labelOriginalPrice.attributedText = strName;
    foot.contentView.backgroundColor = [UIColor whiteColor];
    return foot;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableViewDeal) {
        LSZWithdrawalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZWithdrawalTableViewCell" forIndexPath:indexPath];
        LSZWithdrawalRecordMg * mg = nil;
        mg = self.areDealingManager.arrayData[indexPath.section];
        cell.labelTitle.text = NSStr(mg.title);
        cell.labelTime.text = mg.uptime;
        if ([mg.via isEqualToString:@"微信"]) {
            cell.imageViewType.image = [UIImage imageNamed:@"微信提现"];
        }else if ([mg.via isEqualToString:@"支付宝"]){
            cell.imageViewType.image = [UIImage imageNamed:@"支付宝提现"];
        }
        else{
            cell.imageViewType.image = [UIImage imageNamed:@"手机充值"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZWithdrawalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZWithdrawalTableViewCell" forIndexPath:indexPath];
        LSZWithdrawalRecordMg * mg = nil;
        mg = self.WithdrawalRecordManager.arrayData[indexPath.section];
        cell.labelTitle.text = NSStr(mg.title);
        cell.labelTime.text = mg.uptime;
        if ([mg.via isEqualToString:@"微信"]) {
            cell.imageViewType.image = [UIImage imageNamed:@"微信提现"];
        }else if ([mg.via isEqualToString:@"支付宝"]){
            cell.imageViewType.image = [UIImage imageNamed:@"支付宝提现"];
        }
        else{
            cell.imageViewType.image = [UIImage imageNamed:@"手机充值"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
#pragma mark - 点击进去订单详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LSZDetailsWithdrawalOrderVC * vc = [[LSZDetailsWithdrawalOrderVC alloc]init];
    if (tableView == self.tableViewDeal) {
        LSZWithdrawalRecordMg * mg = nil;
        mg = self.areDealingManager.arrayData[indexPath.section];
        vc.withdrawalRecordMg = mg;
    }else{
        LSZWithdrawalRecordMg * mg = nil;
        mg = self.WithdrawalRecordManager.arrayData[indexPath.section];
        vc.withdrawalRecordMg = mg;
    }
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setLowUI
{
    self.scrollViewVideo = [[UIScrollView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(self.viewTop.frame), ScreenWidth, ScreenHeight- CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT)];
    self.scrollViewVideo.delegate = self;
    self.scrollViewVideo.contentSize = CGSizeMake(ScreenWidth*2, ScreenHeight- CGRectGetMaxY(self.viewTop.frame));
    self.scrollViewVideo.bounces = NO; // 默认为YES 取消设置NO
    // 当bounces属性为NO时，以下两个属性设置无效
    self.scrollViewVideo.alwaysBounceHorizontal = YES; // 水平方向弹簧效果
    self.scrollViewVideo.alwaysBounceVertical = YES; // 垂直方向弹簧效果
    self.scrollViewVideo.decelerationRate = 0.1; // 值域 0~1 值越小减速停止的时间越短
    self.scrollViewVideo.pagingEnabled = YES; // 默认为NO，设置YES内容视图整页滚动
    //    scrollView.delaysContentTouches = YES;
    self.scrollViewVideo.showsVerticalScrollIndicator = YES; // 垂直方向
    [self.view addSubview:self.scrollViewVideo];

    self.tableViewDeal = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight- CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableViewDeal.delegate = self;
    self.tableViewDeal.dataSource = self;
    self.tableViewDeal.estimatedRowHeight=0;
    self.tableViewDeal.estimatedSectionFooterHeight =0;
    self.tableViewDeal.estimatedSectionHeaderHeight =0;
    self.tableViewDeal.separatorColor = divider;
    [self.scrollViewVideo addSubview:self.tableViewDeal];
    [self.tableViewDeal registerClass:[LSZWithdrawalTableViewCell class] forCellReuseIdentifier:@"LSZWithdrawalTableViewCell"];
    [self.tableViewDeal registerClass:[LSZWithdrawalFootView class] forHeaderFooterViewReuseIdentifier:@"LSZWithdrawalFootView"];
    [self.tableViewDeal registerClass:[LSZWithdrawalHeaderView class] forHeaderFooterViewReuseIdentifier:@"LSZWithdrawalHeaderView"];

    self.tableViewComplete = [[UITableView alloc]initWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight- CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableViewComplete.delegate = self;
    self.tableViewComplete.dataSource = self;
    self.tableViewComplete.estimatedRowHeight=0;
    self.tableViewComplete.estimatedSectionFooterHeight =0;
    self.tableViewComplete.estimatedSectionHeaderHeight =0;
    self.tableViewComplete.separatorColor = divider;
    [self.scrollViewVideo addSubview:self.tableViewComplete];
    [self.tableViewComplete registerClass:[LSZWithdrawalTableViewCell class] forCellReuseIdentifier:@"LSZWithdrawalTableViewCell"];
    [self.tableViewComplete registerClass:[LSZWithdrawalFootView class] forHeaderFooterViewReuseIdentifier:@"LSZWithdrawalFootView"];
    [self.tableViewComplete registerClass:[LSZWithdrawalHeaderView class] forHeaderFooterViewReuseIdentifier:@"LSZWithdrawalHeaderView"];
}
-(void)setTopView
{
    self.viewTop = [[UIView alloc]init];
    self.viewTop.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewTop];
    if(ScreenWidth ==414){
        self.viewTop.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth, 60);
    }else
    if(ScreenWidth ==375){
        self.viewTop.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth, 60);
    }else{
        self.viewTop.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth, 50);
    }
    self.butAreDealingWith = [[UIButton alloc]init];
    [self.butAreDealingWith setTitle:@"正在处理" forState:UIControlStateNormal];
    [self.butAreDealingWith setTitleColor:navColor forState:UIControlStateNormal];
    self.butAreDealingWith.tag = 1;
    [self.butAreDealingWith addTarget:self action:@selector(clickButChooseWithdrawal:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:self.butAreDealingWith];
    self.butAreDealingWith.frame = CGRectMake(0, 0, self.viewTop.frame.size.width/2,self.viewTop.frame.size.height);
    if(ScreenWidth ==414){
        self.butAreDealingWith.titleLabel.font = FONT_16;
    }else
    if(ScreenWidth ==375){
        self.butAreDealingWith.titleLabel.font = FONT_16;
    }else{
        self.butAreDealingWith.titleLabel.font = FONT_14;
    }
    self.butComplete = [[UIButton alloc]init];
    self.butComplete.titleLabel.font = FONT_14;
    [self.butComplete setTitle:@"全部订单" forState:UIControlStateNormal];
    [self.butComplete setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    self.butComplete.tag = 2;
    [self.butComplete addTarget:self action:@selector(clickButChooseWithdrawal:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:self.butComplete];
    self.butComplete.frame = CGRectMake(self.viewTop.frame.size.width/2, 0, self.viewTop.frame.size.width/2,self.viewTop.frame.size.height);
    if(ScreenWidth ==414){
        self.butComplete.titleLabel.font = FONT_16;
    }else
    if(ScreenWidth ==375){
        self.butComplete.titleLabel.font = FONT_16;
    }else{
        self.butComplete.titleLabel.font = FONT_14;
    }
    UIView * viewSmall = [[UIView alloc]initWithFrame:CGRectMake(0, self.viewTop.frame.size.height-1, self.viewTop.frame.size.width, 1)];
    viewSmall.backgroundColor = divider;
    [self.viewTop addSubview:viewSmall];

    UIView * viewVerticalBar = [[UIView alloc]initWithFrame:CGRectMake(self.viewTop.frame.size.width/2, 10, 1, self.viewTop.frame.size.height-20)];
    viewVerticalBar.backgroundColor = divider;
    [self.viewTop addSubview:viewVerticalBar];

    self.chooseMoveView = [[UIView alloc]init];
    self.chooseMoveView.backgroundColor = navColor;
    [self.viewTop addSubview:self.chooseMoveView];
    self.chooseMoveView.frame = CGRectMake(ScreenWidth/6,self.viewTop.frame.size.height-2, ScreenWidth/6, 2);
}
-(void)clickButChooseWithdrawal:(UIButton *)button
{
    if (button.tag == 1) {
        [self clickChooseOne];
    }else{
        [self clickChooseTwo];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.tableViewDeal || scrollView == self.tableViewComplete) {
        return;
    }
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    if (index == 0) {
        [self  clickChooseOne];
    }
    else{
        [self clickChooseTwo];
    }

}
-(void)clickChooseOne
{
    self.chooseMoveView.frame = CGRectMake(ScreenWidth/6,self.viewTop.frame.size.height-2,ScreenWidth/6, 2);
    [self.butAreDealingWith setTitleColor:navColor forState:UIControlStateNormal];
    [self.butComplete setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [self.scrollViewVideo setContentOffset:CGPointMake(0, 0) animated:YES];
    if (self.areDealingManager.arrayData.count ==0) {
        [self initWithtring:@"暂无订单!"];
    }
}
-(void)clickChooseTwo
{
    self.chooseMoveView.frame = CGRectMake(ScreenWidth/6*4,self.viewTop.frame.size.height-2, ScreenWidth/6, 2);
    [self.butAreDealingWith setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [self.butComplete setTitleColor:navColor forState:UIControlStateNormal];
    [self.scrollViewVideo setContentOffset:CGPointMake(ScreenWidth, 0) animated:YES];
    if (self.WithdrawalRecordManager.arrayData.count ==0) {
        [self initWithtring:@"您还没有订单记录!"];
    }
}
-(void)getDataWithdrawal
{
    [LSZRequestResolutionDic AlipayOrWeChatWithdrawal :@"9" :^(BOOL isok, NSString * str, LSZWithdrawalRecordManager * mg) {
        if (isok == YES) {
            self.areDealingManager = mg;
            if(mg.arrayData.count >0){
                [self.tableViewDeal reloadData];
            }else{
                [self initWithtring:@"您还没有订单记录!"];
            }
        }else{
            [self setStringWarning:str];
        }
    }];
    [LSZRequestResolutionDic AlipayOrWeChatWithdrawal :@"" :^(BOOL isok, NSString * str, LSZWithdrawalRecordManager * mg) {
        if (isok == YES) {
            self.WithdrawalRecordManager = mg;
            if(mg.arrayData.count >0){
                [self.tableViewComplete reloadData];
            }else{
            }
        }else{
            [self setStringWarning:str];
        }
    }];
}
@end
